package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.R;
import com.example.entity.User;
import com.example.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/loginUser")
    public R login(HttpServletRequest request, @RequestBody User user){


        LambdaQueryWrapper<User> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(user.getEmail()),User::getEmail,user.getEmail());

        User user1 = userService.getOne(lqw);

        if (user1 != null) {
            request.getSession().setAttribute("user",user1);
            return R.success("登录成功！",user1);

        }else {
            return R.error("登录失败！");
        }
    }



    /**
     * 分页查询所有user信息
     * @return
     */
    @GetMapping("/getPageUser/{currentPage}/{pageSize}")

    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,User user){

        IPage page=new com.baomidou.mybatisplus.extension.plugins.pagination.Page(currentPage,pageSize);
        LambdaQueryWrapper<User> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(user.getUsername()),User::getUsername,user.getUsername());

        IPage page1 = userService.page(page,lqw);

        return R.success("分页查询所有user成功！",page1);
    }

    /**
     * 查询当前user
     * @return
     */

    @GetMapping("/getMyself/{id}")

    public R getOne(@PathVariable int id){

        User user= userService.getById(id);

        return R.success("查询当前user成功！",user);
    }


    /**
     * 添加
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public R add(@RequestBody User user){


        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(user.getEmail()),User::getEmail,user.getEmail());
        User user1=userService.getOne(lqw);

        if (user1==null) {
            Boolean update = userService.save(user);
            if (update) {
                return R.success("添加user成功!", null);
            }
        }
        return R.error("邮箱已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = userService.removeById(id);
        if (byId){
            return R.success("删除user成功!",null);
        }
        return R.error("删除user失败!");
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    public R deleteById(@RequestBody User user){

        Boolean update = userService.updateById(user);
        if (update){
            return R.success("修改user成功!",null);
        }
        return R.error("修改user失败!");
    }

    /**
     * 通过id修改用户账号状态
     * @return
     */
    @PutMapping("/updateUserStatus")
    public R updateUserStatus(@RequestBody User user){
        boolean b = userService.updateById(user);
        if (b){
            return R.success("通过id修改用户账号状态成功",user);
        }else {
            return R.error("通过id修改用户账号状态失败！");
        }
    }




}

